package BinaryTree;

/**
 * @Author zhang lei
 * @Date 2021-11-25 18:36
 */
public class Algo1To10 {

    public static void main(String[] args) {
        System.out.println(addBinary("1010", "1011"));
    }

    /**
     * Algo 二进制加法
     * @param a
     * @param b
     * @return
     */
    public static String addBinary(String a, String b) {
        int i= a.length()-1;
        int j = b.length() - 1;

        StringBuilder sb = new StringBuilder();
        int add = 0;
        while (i>=0 || j>=0) {

            int aC = i>=0? a.charAt(i--) - '0':0;
            int bC = j>=0? b.charAt(j--) - '0':0;

            int sum = aC + bC + add;
            add = sum/2;
            sum = sum %2;

            sb.append(sum);
        }
        if(add>0) {
            sb.append(add);
        }
        return sb.reverse().toString();
    }
}
